package cn.doit.scala.day04

import scala.io.Source

/**
 * @DATE 2021/12/28/10:53
 * @Author MDK
 * @Version 2021.2.2
 * */
object C08_SameFriends {
  def main(args: Array[String]): Unit = {
    val lines = Source.fromFile("data/friends.txt").getLines()
//    lines.foreach(println)
    //处理每行数据  将数据封装在Map集合中
//    Map[String, Array[String]]
    val tuples = lines.map(line => {
      val arr: Array[String] = line.split(":")
      val name: String = arr(0)
      val friends = arr(1).split(",")
      (name, friends)
    }).toArray

//    tuples.foreach(tp=>println((tp._1,tp._2.toList)))
    for(i <- 0 until tuples.length - 1){
      val name = tuples(i)._1
      val fs1:Array[String] = tuples(i)._2
      for(j <- i+1 to tuples.length-1){
        val name2: String = tuples(j)._1
        val fs2: Array[String] = tuples(j)._2
        //集合的交集
        val same = fs1.intersect(fs2).toList
        val res = (s"$name 和 $name2 的好友是"+same)
        if(same.length>0){
          println(res)
        }
      }
    }
  }
}
